
package com.gofunbuy.app.favourable;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.gofunbuy.R;
import com.gofunbuy.TabFavourableGroup;
import com.gofunbuy.TabStoreGroup;
import com.gofunbuy.core.CouponEntity;
import com.gofunbuy.http.HttpClientImpl;
import com.gofunbuy.util.GoFunBuyUtils;
import com.gofunbuy.util.XmlUtil;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class TabFavourableActivity extends Activity implements OnClickListener{
	TextView favourable_diet ;
	TextView favourable_private;
	TextView favourable_activity;
	TextView favourable_birth ;
	
	private List<CouponEntity> dietCoupons;
	private List<CouponEntity> privateCoupons;
	private List<CouponEntity> activityCoupons;
	private List<CouponEntity> birthCoupons;
	private List<CouponEntity> coupons;
	private ListView mCouponlist;
	private AppFavourableAdapter mAdapter;
	private ProgressBar mProgressBar;
	public  static int tabId;
	public  static String couponId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.favourable_layout);
		init();
		dietCoupons = new ArrayList<CouponEntity>();
		privateCoupons = new ArrayList<CouponEntity>();
		activityCoupons = new ArrayList<CouponEntity>();
		birthCoupons = new ArrayList<CouponEntity>();
		new LoadTask().execute();	
	}
	private void init(){
		TextView buyList = (TextView) findViewById(R.id.store_topbar_buylist);
		buyList.setVisibility(View.GONE);
		
		TextView tvTitle = (TextView) findViewById(R.id.store_topbar_title);
		tvTitle.setText(R.string.title_favorable);
		

		TextView back = (TextView)findViewById(R.id.store_topbar_back);
		back.setVisibility(View.GONE);
		
		mCouponlist = (ListView)findViewById(R.id.coupon_list);
		mAdapter = new AppFavourableAdapter(this,mCouponlist);
		mCouponlist.setAdapter(mAdapter);
		
		mCouponlist.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				couponId = coupons.get(arg2).getId();
				startActivity(SubTabFavourableDetailActivity.class);
			}			
		});
		
		 favourable_diet  = (TextView) findViewById(R.id.favourable_diet);
		 favourable_private = (TextView) findViewById(R.id.favourable_private);
		 favourable_activity = (TextView) findViewById(R.id.favourable_activity);
		 favourable_birth = (TextView) findViewById(R.id.favourable_birth);
		favourable_diet.setOnClickListener(this);
		favourable_private.setOnClickListener(this);
		favourable_activity.setOnClickListener(this);
		favourable_birth.setOnClickListener(this);
		
		mProgressBar = (ProgressBar)findViewById(R.id.favourable_progress);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setSubTabColor(tabId);
		
		switch (tabId) {
		case 0:
			
			mAdapter.setData(dietCoupons);
			mAdapter.notifyDataSetChanged();
			
			break;
		case 1:
			mAdapter.setData(privateCoupons);
			mAdapter.notifyDataSetChanged();
			
			break;
		case 2:
			mAdapter.setData(activityCoupons);
			mAdapter.notifyDataSetChanged();
			
			break;
		case 3:
			mAdapter.setData(birthCoupons);
			mAdapter.notifyDataSetChanged();
			
			break;		
		}	
	}
	private List<CouponEntity> getCoupons(){
		String uri = "http://www.eye4gift.com/gofun/app_get_coupon.php";
		String userId = GoFunBuyUtils.mUserID==null?"0":GoFunBuyUtils.mUserID;	
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("shopcode", "goinside");
		params.put("userid", userId);
		InputStream is = HttpClientImpl.HttpPostData2(uri, params);
		List<CouponEntity> coupons = XmlUtil.getCouponList(is);		
		return coupons;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		return super.onKeyDown(keyCode, event);
	}
	private void startActivity(Class<?> a,Bundle b) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(this, a);
		intent.putExtras(b);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabFavourableGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabFavourableGroup.group.setContentView(view);
		
	}
	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabFavourableGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabFavourableGroup.group.setContentView(view);
		
	}
	private void setSubTabColor(int tab){
		 tabId = tab;
		 favourable_diet.setTextColor(Color.WHITE) ;
		 favourable_private.setTextColor(Color.WHITE) ;
		 favourable_activity.setTextColor(Color.WHITE) ;
		 favourable_birth.setTextColor(Color.WHITE) ;
		switch (tab) {
		case 0:
			 favourable_diet.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 1:
			favourable_private.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 2:
			favourable_activity.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 3:
			favourable_birth.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;		
		}
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
		switch (view.getId()) {
		case R.id.favourable_diet:
			setSubTabColor(0);
			mAdapter.setData(dietCoupons);
			mAdapter.notifyDataSetChanged();
			
			break;
		case R.id.favourable_private:
			mAdapter.setData(privateCoupons);
			mAdapter.notifyDataSetChanged();
			setSubTabColor(1);
			break;
		case R.id.favourable_activity:
			mAdapter.setData(activityCoupons);
			mAdapter.notifyDataSetChanged();
			setSubTabColor(2);
			break;
		case R.id.favourable_birth:
			mAdapter.setData(birthCoupons);
			mAdapter.notifyDataSetChanged();
			setSubTabColor(3);
			break;		
		}
	}
    private class LoadTask extends AsyncTask<Void, Void, Void> { 
    	
        @Override  
        protected void onPreExecute() {  
        	if(!GoFunBuyUtils.isNetworkConnected(TabFavourableGroup.group)){
    			Toast.makeText(TabFavourableGroup.group, "网络状况不好！", 2000).show();
    			
    			this.cancel(true);
    			return;
    		}
        	mProgressBar.setVisibility(View.VISIBLE);
        	mCouponlist.setVisibility(View.GONE);
        }            
        @Override  
        protected Void doInBackground(Void... params) {   
        	coupons = getCoupons();
            return null;  
        }                   
          
        @Override  
        protected void onPostExecute(Void result) {  

        	mProgressBar.setVisibility(View.GONE);
        	mCouponlist.setVisibility(View.VISIBLE);
 
    		if(coupons!=null){
    			for(int i=0;i<coupons.size();i++){
    				if(coupons.get(i).getTypeName().equals("飲食優惠")){
    					dietCoupons.add(coupons.get(i));
    				}else if(coupons.get(i).getTypeName().equals("包場優惠")){
    					privateCoupons.add(coupons.get(i));
    				}else if(coupons.get(i).getTypeName().equals("活動優惠")){
    					activityCoupons.add(coupons.get(i));
    				}else if(coupons.get(i).getTypeName().equals("生日優惠")){
    					birthCoupons.add(coupons.get(i));
    				}
    			}
    		}

    		setSubTabColor(tabId);
    		
    		switch (tabId) {
    		case 0:
    			
    			mAdapter.setData(dietCoupons);
    			mAdapter.notifyDataSetChanged();
    			
    			break;
    		case 1:
    			mAdapter.setData(privateCoupons);
    			mAdapter.notifyDataSetChanged();
    			
    			break;
    		case 2:
    			mAdapter.setData(activityCoupons);
    			mAdapter.notifyDataSetChanged();
    			
    			break;
    		case 3:
    			mAdapter.setData(birthCoupons);
    			mAdapter.notifyDataSetChanged();
    			
    			break;		
    		}
        }   
        @Override  
        protected void onCancelled() {  
        	//dialog.dismiss();
        }  
    }  
}
